﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using QuadRemote.Core;

namespace QuadRemote.Panels
{
    class CompassPanel : QuadPanel
    {
        public CompassPanel()
            : base("Compass", 180, 180, Color.Blue,25)
        {
            lockAspect = true;
        }

        public override void PaintPanel(Graphics g)
        {
            int radius = PanelWidth/2;

            for (int d = 0; d < 360; d += 15)
            {
                if (d % 90 == 0) continue;
                float sin = (float)Math.Sin(d * Math.PI / 180.0);
                float cos = -(float)Math.Cos(d * Math.PI / 180.0);

                g.DrawLine(Pens.White, radius + sin * radius, radius + cos * radius, radius + sin * (radius - radius * 0.1F), radius + cos * (radius - radius * 0.1F));
            }

            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            g.DrawString("N", new Font("Calibri", radius / 12), Brushes.White, radius, radius * 0.05F, format);
            g.DrawString("S", new Font("Calibri", radius / 12), Brushes.White, radius, radius * 2 - radius * 0.05F, format);
            g.DrawString("W", new Font("Calibri", radius / 12), Brushes.White, radius*0.05F, radius, format);
            g.DrawString("E", new Font("Calibri", radius / 12), Brushes.White, radius * 2 - radius * 0.05F, radius, format);


            float heading = 0;// RemoteController.instance().latestState.yaw;

            Matrix m = new Matrix();
            m.RotateAt(heading, new PointF(radius + margin, radius + margin));
            m.Translate(margin, margin);
            g.Transform = m;

            Pen p = Pens.White;
            g.DrawLine(p, radius, radius * 0.15F, radius - radius * 0.3F, radius * 0.6F);
            g.DrawLine(p, radius, radius * 0.15F, radius + radius * 0.3F, radius * 0.6F);

            g.DrawLine(p, radius - radius * 0.17F, radius * 0.6F, radius - radius * 0.3F, radius * 0.6F);
            g.DrawLine(p, radius + radius * 0.17F, radius * 0.6F, radius + radius * 0.3F, radius * 0.6F);

            g.DrawLine(p, radius - radius * 0.17F, radius * 0.6F, radius - radius * 0.17F, radius * 2 - radius * 0.4F);
            g.DrawLine(p, radius + radius * 0.17F, radius * 0.6F, radius + radius * 0.17F, radius * 2 - radius * 0.4F);

            float h = (float)heading % 360;
            while (h < 0) h += 360;

            m = new Matrix();
            int offset = heading < 180 ? -90 : 90;
            m.RotateAt(heading+offset, new PointF(radius + margin, radius + margin));
            m.Translate(margin, margin);
            g.Transform = m;

            g.DrawString(Math.Round(h,1) + "°", new Font("Calibri", radius / 7), Brushes.White, radius, radius, format);

        }
    }
}
